<!-- 编辑弹窗 -->
<template>
  <ele-modal :form="true" :destroy-on-close="true" :width="460" v-model="visible" :title="isUpdate ? '修改客户列' : '添加客户列'">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" @submit.prevent="">
      <el-form-item label="客户名称" prop="customerName">
        <el-input clearable v-model="form.customerName" placeholder="请输入客户名称" />
      </el-form-item>
      <el-form-item label="所属组织" prop="deptId">
        <dept-select v-model="form.deptId" placeholder="请选择所属组织" />
      </el-form-item>
      <el-form-item label="微信号" prop="wxCode">
        <el-input clearable v-model="form.wxCode" placeholder="请输入微信号" />
      </el-form-item>
      <!-- <el-form-item label="最后跟进时间" prop="createdBy">
        <el-input clearable v-model="form.createdBy" placeholder="请输入创建人" />
      </el-form-item> -->
      <el-form-item label="累计服务次数" prop="totalServiceCount">
        <el-input clearable v-model="form.totalServiceCount" placeholder="请输入累计服务次数" />
      </el-form-item>
      <el-form-item label="累计服务时长" prop="totalServiceDuration">
        <el-input clearable v-model="form.totalServiceDuration" placeholder="请输入累计服务时长(秒)" />
      </el-form-item>
      <el-form-item label="最近跟进员工" prop="userId">
        <el-input clearable v-model="form.userId" placeholder="请输入最近跟进员工" />
      </el-form-item>
      <el-form-item label="备注信息" prop="notes">
        <el-input :rows="4" type="textarea" v-model="form.notes" placeholder="请输入备注信息" />
      </el-form-item>
      <!-- <el-form-item label="创建人" prop="createdBy">
        <el-input clearable v-model="form.createdBy" placeholder="请输入创建人" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker v-model="form.createdTime" value-format="YYYY-MM-DD" placeholder="请选择创建时间" class="ele-fluid" />
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy">
        <el-input clearable v-model="form.updateBy" placeholder="请输入修改人" />
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-date-picker v-model="form.updateTime" value-format="YYYY-MM-DD" placeholder="请选择修改时间" class="ele-fluid" />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { EleMessage } from 'ele-admin-plus';
import { useFormData } from '@/utils/use-form-data';
import { addEtpCustomerServiceStats, updateEtpCustomerServiceStats } from '@/api/etp/etpCustomerServiceStats';
import DeptSelect from '@/views/system/dept/components/dept-select.vue';

const props = defineProps({
  /** 修改回显的数据 */
  data: Object
});

const emit = defineEmits(['done']);

/** 弹窗是否打开 */
const visible = defineModel({ type: Boolean });

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  id: void 0,
  customerName: '',
  deptId: void 0,
  userId: void 0,
  totalServiceCount: void 0,
  totalServiceDuration: void 0,
  wxCode: '',
  notes: '',
  createdBy: '',
  createdTime: '',
  updateBy: '',
  updateTime: ''
});

/** 表单验证规则 */
const rules = reactive({
  customerName: [
    {
      required: true,
      message: '请输入客户名称',
      type: 'string',
      trigger: 'blur'
    }
  ]
});

/** 关闭弹窗 */
const handleCancel = () => {
  visible.value = false;
};

/** 保存编辑 */
const save = () => {
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updateEtpCustomerServiceStats : addEtpCustomerServiceStats;
    saveOrUpdate(form)
      .then((msg) => {
        loading.value = false;
        EleMessage.success({ message: msg, plain: true });
        handleCancel();
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error({ message: e.message, plain: true });
      });
  });
};

/** 监听弹窗打开 */
watch(visible, () => {
  if (visible.value) {
    if (props.data) {
      assignFields(props.data);
      isUpdate.value = true;
    } else {
      resetFields();
      isUpdate.value = false;
    }
  }
});
</script>
